package newPan3d.content.scene
{
	import _Pan3D.core.MathCore;
	
	import _me.Scene_data;
	
	import com.adobe.utils.PerspectiveMatrix3D;
	
	import editAir.water.WaterLevel;
	
	import flash.display.BitmapData;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DBlendFactor;
	import flash.display3D.Context3DCompareMode;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DTriangleFace;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.BlurFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import newPan3d.NewIndex;
	import newPan3d.NewKeyControl;
	import newPan3d.content.build.BuildLevel;
	import newPan3d.content.frie.FrieLevel;
	import newPan3d.content.glass.GlassLevel;
	import newPan3d.content.ground.GroundLevel;
	import newPan3d.content.ground.groundUp.GroundUpLevel;
	import newPan3d.content.ground.map.MapClass;
	import newPan3d.content.jineng.HuoqiuLevel;
	import newPan3d.content.lightScene.LightMenager;
	import newPan3d.content.npc.NpcLevel;
	import newPan3d.content.role.HeroLevle;
	import newPan3d.content.skill.SkillLevel;
	import newPan3d.content.tree.TreeLevel;
	import newPan3d.content.tree.luoye.LuoYeLevel;

	public class SceneLevel
	{
		public var _groundView:GroundLevel
		public var _groundUpLevel:GroundUpLevel
		public var _buildView:BuildLevel
		public var _treeLevel:TreeLevel;
		public var _glassLevel:GlassLevel;
		public var _frieLevel:FrieLevel;
		public var _luoYeLevel:LuoYeLevel;
		public var _huoqiuLevel:HuoqiuLevel;
		public var _lightMenager:LightMenager;
		public var _skillLevel:SkillLevel;
		public var _waterLevel:WaterLevel;

		public var _heorLevle:HeroLevle;
		public var _npcLevel:NpcLevel;

		public var _projMatrix:PerspectiveMatrix3D;
		public var _keyControl:NewKeyControl=new NewKeyControl

		public function SceneLevel()
		{
		}

		public function initData():void
		{
			SceneMenager.initData();
			MapClass.initData();
			Scene_data.funToScanShodar=getShadowBitMapData; //加载完所有资源，就扫描阴影
			makeScene();
			_keyControl.init(Scene_data.stage);
		}


		public function makeScene():void
		{
			_groundView=new GroundLevel();
			_groundUpLevel=new GroundUpLevel()
			_buildView=new BuildLevel();
			_treeLevel=new TreeLevel();
			_glassLevel=new GlassLevel();
			_frieLevel=new FrieLevel();
			_heorLevle=new HeroLevle();
			_npcLevel=new NpcLevel();
			_luoYeLevel=new LuoYeLevel();
			_lightMenager=new LightMenager();
			_huoqiuLevel=new HuoqiuLevel();
			_skillLevel = new SkillLevel();
		
		}

		public function upData():void
		{
			if (!Scene_data.ready)
				return;

	    	_lightMenager.updata();
			//Scene_data.groundLightText=Scene_data.sceneLightText
			var context3D:Context3D=Scene_data.context3D;

			MathCore._catch_cam(Scene_data.cam3D, Scene_data.focus3D);
			context3D.clear(0, 0, 0, 1);
			_projMatrix=new PerspectiveMatrix3D();
			_projMatrix.perspectiveFieldOfViewLH(1, 1, 0.1, 9000);
			context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, _projMatrix, true);
			context3D.setBlendFactors(Context3DBlendFactor.ONE, Context3DBlendFactor.ZERO);
			context3D.setDepthTest(true, Context3DCompareMode.LESS);
			_groundView.upData();
//			context3D.setDepthTest(true, Context3DCompareMode.ALWAYS);
//			_groundUpLevel.upData();
//			context3D.setDepthTest(true, Context3DCompareMode.LESS);
			_buildView.upData();
			_treeLevel.upData();
			_heorLevle.upData();
			_glassLevel.upData();
			_npcLevel.upData();
			//_luoYeLevel.upData();
		
			


			context3D.setCulling(Context3DTriangleFace.NONE);
			context3D.setBlendFactors(Context3DBlendFactor.SOURCE_ALPHA, Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA);
			context3D.setDepthTest(false, Context3DCompareMode.LESS);
			_huoqiuLevel.upData();
//			_frieLevel.upData();
			_skillLevel.upData();

			context3D.present();
			SceneMenager.makelight3D();



		}

		public var haveShadow:Boolean=false;

		public function getShadowBitMapData():void
		{
			if (haveShadow)
				return;
			haveShadow=true;
			var context3D:Context3D=Scene_data.context3D;
			var shadowBitmapData:BitmapData=new BitmapData(1024, 1024, false, 0xffffff) //先保证是白的基础色表示没有

			_treeLevel.display3DContainer.scaningShadow(context3D); //得到建筑的阴影
			var EE:BitmapData=_treeLevel.display3DContainer.scaningShadow(context3D); //得到建筑的阴影
			var DD:BitmapData=_buildView.display3DContainer.scaningShadow(context3D); //得到建筑的阴影

			shadowBitmapData.draw(DD);
			shadowBitmapData.draw(EE);

			var filter:BlurFilter=new BlurFilter(3, 3, BitmapFilterQuality.HIGH);
			shadowBitmapData.applyFilter(shadowBitmapData, new Rectangle(0, 0, shadowBitmapData.width, shadowBitmapData.height), new Point, filter);


		//	NewIndex(Scene_data.root).uiView.shadowBmp.bitmapData=shadowBitmapData;
		//	NewIndex(Scene_data.root).uiView.shadowBmp.bitmapData=shadowBitmapData;

			Scene_data.texShadowMap.uploadFromBitmapData(shadowBitmapData);
			stageResize()
			context3D.setCulling(Context3DTriangleFace.NONE);

		}

		public function stageResize():void
		{
			Scene_data.cam3D.fovw=Scene_data.stageWidth;
			Scene_data.cam3D.fovh=Scene_data.stageHeight;
			var context3D:Context3D=Scene_data.context3D;
			context3D.configureBackBuffer(Scene_data.stageWidth, Scene_data.stageHeight, 0, true);
		}
	}
}
